package com.xcc.buglytest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhoubin on 2018/1/10.
 */

public class DeviceUtil {
    public static String getAndroidID() {
        return Settings.Secure.getString(AppTest.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 获取版本号
     *
     * @return
     */
    public static String getAppVerName(Context context) {
        String versionName = "Err";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_ACTIVITIES);
            if (pi != null && !TextUtils.isEmpty(pi.versionName)) versionName = pi.versionName;
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取版本API
     *
     * @return
     */
    public static int getAppVerApi(Context context) {
        int versionName = 0;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_ACTIVITIES);
            if (pi != null) versionName = pi.versionCode;
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 判断是否平板设备
     *
     * @param context
     * @return true:平板,false:手机
     */
    public static boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >=
                Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static DeviceUtil getInstance() {
        if (deviceUtil == null) {
            synchronized (DeviceUtil.class) {
                if (deviceUtil == null) {
                    deviceUtil = new DeviceUtil();
                }
            }
        }
        return deviceUtil;
    }

    private static DeviceUtil deviceUtil;
    private String client, clientType, clientName, clientName_;
    private String CPU_ABI;
    private String UA;

    public synchronized String getUA() {
        if (TextUtils.isEmpty(UA)) {
            UA = "BulaEnglish_AndroidApp_" + getClientName_();
        }
        return UA;
    }

    public String getClient() {
        if (TextUtils.isEmpty(client)) {
            client = "app";
        }
        return client;
    }

    public String getClientType() {
        if (TextUtils.isEmpty(clientType)) {
            clientType = "android";
        }
        return clientType;
    }

    public String getClientName_() {
        if (TextUtils.isEmpty(clientName_)) {
            String msg = "";
            Map<String, String> info = collectDeviceInfo(AppTest.getInstance());
            String s = info.get("versionName");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("BRAND");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("MODEL");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("OS_API");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("CPU_ABI");
            if (!TextUtils.isEmpty(s)) {
                if (TextUtils.isEmpty(CPU_ABI)) CPU_ABI = s;
                msg += s;
            }
            clientName_ = msg;
        }
        return clientName_;
    }

    public String getClientName() {
        if (TextUtils.isEmpty(clientName)) {
            String msg = "";
            Map<String, String> info = collectDeviceInfo(AppTest.getInstance());
            String s = info.get("BRAND");
            if (!TextUtils.isEmpty(s)) msg = s + "_";
            s = info.get("MODEL");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("OS_API");
            if (!TextUtils.isEmpty(s)) msg += s + "_";
            s = info.get("CPU_ABI");
            if (!TextUtils.isEmpty(s)) {
                if (TextUtils.isEmpty(CPU_ABI)) CPU_ABI = s;
                msg += s + "_";
            }
            s = info.get("versionName");
            if (!TextUtils.isEmpty(s)) msg += s;
            clientName = msg;
        }
        return clientName;
    }

    public String getCPU_ABI() {
        if (TextUtils.isEmpty(CPU_ABI)) {
            Map<String, String> info = collectDeviceInfo(AppTest.getInstance());
            String s = info.get("CPU_ABI");
            CPU_ABI = s;
        }
        return CPU_ABI;
    }

    public Map<String, String> collectDeviceInfo(Context ctx) {
        Map<String, String> infos = new HashMap<>();
        infos.put("OS_API", "API" + Build.VERSION.SDK_INT);
        infos.put("versionName", getAppVerName(ctx));
        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                String v = field.get(null).toString();
                infos.put(field.getName(), v);
                if (Logger.isDebug)
                    Logger.log("--infos--", field.getName() + " : " + field.get(null));
            } catch (Exception e) {
                if (Logger.isDebug) e.printStackTrace();
            }
        }
        return infos;
    }
}
